#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configparser

# 写入配置文件
# config = configparser.ConfigParser()
# config["DEFAULT"] = {'ServerAliveInterval': '45',
#                       'Compression': 'yes',
#                      'CompressionLevel': '9'}
#
# config['bitbucket.org'] = {}
# config['bitbucket.org']['User'] = 'hg'
# config['topsecret.server.com'] = {}
# topsecret = config['topsecret.server.com']
# topsecret['Host Port'] = '50022'     # mutates the parser
# topsecret['ForwardX11'] = 'no'  # same here
# config['DEFAULT']['ForwardX11'] = 'yes'
# with open('example.ini','w') as configfile:
#     config.write(configfile)


config = configparser.ConfigParser()
print(config.sections())
config.read('example.ini')
print(config.sections())#除了默认的
print(config.defaults())#默认的
print('bitbucket.org' in config)#true

print("-------for------------")
#for循环 默认的也会跟着打印出来
for key in config['bitbucket.org']:
     print(key)
print("-------------------")


print(config.items('topsecret.server.com'))#获取指定节点下所有的键值对
print( config.options('topsecret.server.com'))#获取指定节点下所有的建
print(config.get('topsecret.server.com', 'host port'))#获取指定节点下指定key的值

#检查
print(config.has_section('topsecret.server.com'))

#修改
config.remove_option('bitbucket.org','user')
config.write(open('example.ini','w'))

#设置
config.set('bitbucket.org','useranme','sunlong')
config.write(open('example.ini','w'))